package com.mao.dao.organization;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.mao.dao.BaseDAOHibernate;
import com.mao.model.organization.Organization;
import com.mao.model.organization.OrganizationClass;
import com.mao.model.product.ProductClass;

@Component("organizationClassDao")
public class OrganizationClassDao extends BaseDAOHibernate {
	public List<OrganizationClass> getAllOrganizationClass(Long storeid) {
		Session session = this.getSession();
		String hql = "from OrganizationClass t where t.parent.id is null and t.status = "
				+ OrganizationClass.STATUS_NORMAL
				+ " and t.store.id="
				+ storeid;
		Query query = session.createQuery(hql);
		List<OrganizationClass> list = query.list();
		OrganizationClass ocRes = null;
		if(list != null && list.size() > 0){
			for(int i=0; i<list.size(); i++){
				ocRes = list.get(i);
				if (!Hibernate.isInitialized(ocRes.getParent())) {
					if(ocRes.getParent().getId() != 0)
						Hibernate.initialize(ocRes.getParent());
				}
			}
		}
		return query.list();
	}

	public OrganizationClass saveOrganizationClass(OrganizationClass oc) {
		getHibernateTemplate().save(oc);
		return oc;
	}

	public OrganizationClass updateOrganizationClass(OrganizationClass oc) {
		getHibernateTemplate().merge(oc);
		return oc;
	}

	public OrganizationClass getOrganizationClassById(long id) {
		Session session = this.getSession();
		String hql = "From OrganizationClass oc where oc.status=:status and oc.id=:id";
		Query query = session.createQuery(hql);
		query.setParameter("status", OrganizationClass.STATUS_NORMAL);
		query.setParameter("id", id);
		List<OrganizationClass> list = query.list();
		OrganizationClass ogc = null;
		if (list.size() > 0) {
			ogc = list.get(0);
			Hibernate.initialize(ogc.getLowClass());
			return ogc;
		}
		return null;
	}

	public String valDelOrgClass(long id) {
		Session session = this.getSession();
		String hql = "from OrganizationClass oc where oc.status=:status and oc.id=:id";
		Query query = session.createQuery(hql);
		query.setParameter("id", id);
		query.setParameter("status", OrganizationClass.STATUS_NORMAL);
		List<OrganizationClass> list = query.list();
		OrganizationClass ogc = null;
		if (list != null) {
			ogc = list.get(0);
		}
		if (ogc.getLowClass().size() > 0) {
			return "1";
		}
		Session osession = this.getSession();
		String ohql = "from Organization o where o.status=:status and o.oc.id=:id";
		Query oquery = session.createQuery(ohql);
		oquery.setParameter("id", id);
		oquery.setParameter("status", OrganizationClass.STATUS_NORMAL);
		List<Organization> olist = oquery.list();
		if (olist.size() > 0) {
			return "2";
		}
		return "0";
	}
	
	public List<OrganizationClass> getAllOrganizationClass() {
		Session session = this.getSession();
		String hql = "from OrganizationClass";
		Query query = session.createQuery(hql);
		return query.list();
	}
}
